//
//  ICENavigationController.m
//  ICENavigator
//
//  Created by 邓乐 on 2018/3/5.
//  Copyright © 2018年 Wanda. All rights reserved.
//

#import "ICENavigationController.h"
#import "ICENavigatorProtocol.h"

@interface ICENavigationController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@property (nonatomic, weak) UIViewController* currentVC;

@end

@implementation ICENavigationController

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
    if (self = [super initWithRootViewController:rootViewController]) {
        self.interactivePopGestureRecognizer.delegate = self;
        self.delegate = self;
    }
    return self;
}

//push时隐藏tabBar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    UIViewController* vc = [self topViewController];
    NSDictionary* previousDit = nil;

    
    if ([vc respondsToSelector:@selector(getPublicParams)]) {
        previousDit = [(id<ICENavigatorProtocol>)vc getPublicParams];
    }
    
    if ([viewController respondsToSelector:@selector(setPublicParams:)]) {
        if (previousDit) {
#ifdef DEBUG
            //比较是否有key被覆盖
            NSDictionary* currentDit = nil;
            if ([viewController respondsToSelector:@selector(getPublicParams)]) {
                currentDit = [(id<ICENavigatorProtocol>)viewController getPublicParams];
            }
            [self haveSameKeys:currentDit inClass:NSStringFromClass([viewController class]) and:previousDit inPreviousClass:NSStringFromClass([vc class])];
#endif
            //将上一个类的公共参数合并到当前类的公共参数
            [(id<ICENavigatorProtocol>)viewController setPublicParams:previousDit];
        }
    }
    
    if ([viewController respondsToSelector:@selector(handleTransferPublicParams:)]) {
        NSDictionary* currentDit = nil;
        if ([viewController respondsToSelector:@selector(getPublicParams)]) {
            currentDit = [(id<ICENavigatorProtocol>)viewController getPublicParams];
        }
        [(id<ICENavigatorProtocol>)viewController handleTransferPublicParams:currentDit];
    }
    
#ifdef DEBUG
    else {
        NSLog(@"警告：你的viewcontroller未继承baseviewcontroller:%@", viewController);
    }
#endif
    
    [super pushViewController:viewController animated:animated];
}

- (BOOL)haveSameKeys:(NSDictionary *)currentDit inClass:(NSString*)currentClass and:(NSDictionary *)previousDit inPreviousClass:(NSString*)previousClass
{
    NSSet *set1 = [NSSet setWithArray:[currentDit allKeys]];
    NSSet *set2 = [NSSet setWithArray:[previousDit allKeys]];
    BOOL result = [set1 intersectsSet:set2];
#ifdef DEBUG
    if (result) {
        NSLog(@"公有参数覆盖警告：当前类%@的公有参数:\n%@ \n和上一个类%@的公有参数:\n%@\n有交集", currentClass,set1,previousClass,set2);
    }
#endif
    return result;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        if (self.currentVC == self.topViewController) {
            return YES;
        }
        return NO;
    }
    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController.viewControllers.count == 1) {
        self.currentVC = nil;
    }
    else{
        self.currentVC = viewController;
    }
}
@end
